package register;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.UserDao;
import dao.impl.UserDaoImpl;
import pojo.*;

public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private UserDao userDao = new UserDaoImpl();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String name=request.getParameter("name");
		String salary=request.getParameter("salary");
		String birth=request.getParameter("birth");
		String sex=request.getParameter("sex");
		
		String reg="[0-9]*";
		if(!"".equals(name)&&salary.matches(reg)&&(!"".equals(birth))&&(!"".equals(sex)))
		{
				User u=new User();
				u.setName(name);
				double sal=Double.parseDouble(salary);
				u.setSalary(sal);
				Date date = null;
					try {
						date = new SimpleDateFormat("yyyy-MM-dd").parse(birth);
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				u.setBirth(date);
				u.setSex(Boolean.parseBoolean(sex));
				userDao.addUser(u);
				response.sendRedirect("Show");
				return;
		}
//		response.setContentType("text/html;charset=UTF-8");
//		PrintWriter writer=response.getWriter();
//		writer.write(info);
//		writer.close();
		request.getRequestDispatcher("ErrorServlet").forward(request, response);//
	}

}
